﻿using System;
using System.Data;
using System.Linq;
using System.Text.RegularExpressions;
using PokemonBattle.PokemonData.Rnd;
using PokemonBattle.PokemonData.Util;

namespace PokemonBattle.PokemonData.SQLite.DAL
{
    public static class DALRandomTeam
    {
        /// <summary>
        /// 获取随机用的PmId
        /// </summary>
        /// <returns></returns>
        public static int[] GetRndPmId()
        {
            const string sql = "select * from v_rnd_ids";
            var dt = DbHelperSQLite.ExecuteDataTable(sql);
            var list = from DataRow dr in dt.Rows
                       select dr[0].ToString().ToInt();
            return list.ToArray();
        }

        /// <summary>
        /// 根据PmId获取Pids
        /// </summary>
        /// <returns></returns>
        public static int[] GetPids(int pmId)
        {
            string sql = "select pid from sg_pm_list where pmid=" + pmId;
            var dt = DbHelperSQLite.ExecuteDataTable(sql);
            var list = from DataRow dr in dt.Rows
                       select dr[0].ToString().ToInt();
            return list.ToArray();
        }

        public static PokemonCustomInfo GetSmogonPm(int pid, Random random)
        {
            string sql = "select p.namebase,l.* from sg_pm_list l left join pbo_pm p on p.pmid=l.pmid where l.pid=" + pid;
            var dt = DbHelperSQLite.ExecuteDataTable(sql);
            var dr = dt.Rows[0];
            int pmid = dr["pmid"].ToString().ToInt();
            var data = BattleData.GetPokemon(pmid);
            var pm = new PokemonCustomInfo();
            pm.Identity = data.Identity;
            pm.Nickname = dr["namebase"].ToString();

            byte trait = byte.Parse(dr["trait"].ToString());
            pm.Gender = data.GetGender(random);
            pm.SelectedTrait = (trait == 0) ? data.GetTrait(random) : trait;
            pm.Item = (Item)dr["itemid"].ToString().ToInt();
            pm.Character = (PokemonCharacter)dr["natureid"].ToString().ToInt();
            pm.LV = 100;
            pm.Iv.SetAll(31);

            #region ev & move

            pm.Ev.Hp = dr["hpev"].ToString().ToInt();
            pm.Ev.Atk = dr["atkev"].ToString().ToInt();
            pm.Ev.Def = dr["defev"].ToString().ToInt();
            pm.Ev.SpAtk = dr["spaev"].ToString().ToInt();
            pm.Ev.SpDef = dr["spdev"].ToString().ToInt();
            pm.Ev.Speed = dr["speev"].ToString().ToInt();

            var reg = new Regex(@"觉醒之力 (\d+)", RegexOptions.Compiled);

            for (int i = 0; i < 4; i++)
            {
                string moves = dr["move" + (i + 1)].ToString();
                if (!string.IsNullOrEmpty(moves))
                {
                    var list = from m in moves.Split('/')
                               where !pm.SelectedMoves.Contains(m)
                               select m;

                    var move = list.ToArray().GetRandom(random);
                    if (reg.IsMatch(move))
                    {
                        int typeid = reg.Match(move).Groups[1].Value.ToInt();
                        pm.Iv = DALHiddenPower.GetHiddenIv(typeid);
                        move = "觉醒之力";
                    }
                    pm.SelectedMoves[i] = move;
                }
            }

            #endregion

            return pm;
        }

    }
}
